package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomersDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    //1. 客户列表查询
    @GetMapping
    public Result ListCustomers(CustomersDto customersDto) {
        PageResult<Customer> pageResult = customerService.ListCustomers(customersDto);
     return Result.success(pageResult);
    }
    //2. 新增客户
    @PostMapping
    public Result addCustomer(@RequestBody Customer customer) {
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
        return  Result.success() ;
    }
    //3. 根据id查询 客户
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }
    //4. 修改客户信息
    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }


}
